package cn.mastor.serve.controller;

import cn.mastor.basic.util.LoginContext;
import cn.mastor.org.domain.Employee;
import cn.mastor.org.domain.Shop;
import cn.mastor.org.mapper.EmployyMapper;
import cn.mastor.org.mapper.ShopMapper;
import cn.mastor.serve.domain.ProductDetail;
import cn.mastor.serve.service.IProductDetailService;
import cn.mastor.serve.service.IProductService;
import cn.mastor.serve.domain.Product;
import cn.mastor.serve.query.ProductQuery;
import cn.mastor.basic.util.AjaxResult;
import cn.mastor.basic.util.PageList;
import cn.mastor.user.domain.Logininfo;
import cn.mastor.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    EmployyMapper employyMapper;
    @Autowired
    public IProductService productService;
    @Autowired
    IProductDetailService productDetailService;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    IUserService userService;


    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employyMapper.findBylogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        Shop shop = shopMapper.findById(shopId);
        System.out.println("----------------------"+shop);
        try {
            ProductDetail detail = product.getDetail();
            if( product.getId()!=null)
                productService.update(product);
            else {
                product.setState(0L);
                product.setShop(shop);
                product.setShopId(shopId);
                System.out.println(product.getShopId()+"---------------------");
                productService.add(product);
                detail.setProductId(product.getId());
            }
            productDetailService.add(detail);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);

        } catch (Exception e) {
        e.printStackTrace();
        }
        return AjaxResult.me().success();
    }
	
    //获取用户
    @GetMapping("/bayServe/{id}")
    public AjaxResult bayServe(@PathVariable("id")Long id,HttpServletRequest request)
    {
        return productService.bayServe(id,request);
    }
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.findById(id);
    }
    @GetMapping("/takeOeder/{id}")
    public AjaxResult takeOeder(@PathVariable("id")Long id)
    {
        productService.takeOeder(id);
        return null;
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query, HttpServletRequest request)
    {

        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employyMapper.findBylogininfoId(logininfo.getId());
        if (employee!=null) {
            Long shopId = employee.getShopId();
            query.setShopId(shopId);
        }
        return productService.queryByPage(query);
    }
    @GetMapping("/getProductDetail/{id}")
    public Product getDetail(@PathVariable("id")Long id){
        return productService.getDetail(id);
    }
    @PatchMapping("/dels")
    public void patchdel(@RequestBody List<Long> ids) {
        productService.batchDel(ids);
    }
    @PostMapping("/onsale")
    public AjaxResult onsalePet(@RequestBody List<Long> ids) {
        return productService.onsalePet(ids);
    }
    @PostMapping("/offsale")
    public AjaxResult offsalePet(@RequestBody List<Long> ids) {
        productService.offsale(ids);
        return AjaxResult.me().success();
    }
}
